from . import api
from flask import Flask, render_template
from flask import g, current_app, session, jsonify, request
from dzj.trec_qa import predict_en as dzj_en
from dzj.med_qa import predict_zh as dzj_zh
from hsq import predict_test_en as hsq_en
from hsq import predict_test_zh as hsq_zh
from jy.med_qa import predict_zh as jy_zh
from jy.trec_qa import predict_en as jy_en
from gq import predict_en as gq_en
from gq import predict_zh as gq_zh
from hjl import pred_hjl_en as hjl_en, pred_hjl_zh as hjl_zh

from flask_api.util.response_code import RETCODE


# app = Flask(__name__)


@api.route('/en/<model_id>', methods=["POST"])
def en_model(model_id):
    """
    英文模型接口
    :param model_id:
    :return: JSON数据
    """
    model_id = int(model_id)

    req = request.get_json()
    print(req)
    text = req.get("en_text")
    # text = "reason Why does the moon turn orange ?"
    if model_id == 0:
        # 季杨
        result = jy_en.predict(text)
        return jsonify(code=RETCODE.OK, msg="OK", data=result)
    elif model_id == 1:
        # 胡势权
        result = hsq_en.predict(text)
        return jsonify(code=RETCODE.OK, msg="OK", data=result)
    elif model_id == 2:
        # 何家乐
        result = hjl_en.predict(text)
        return jsonify(code=RETCODE.OK, msg="OK", data=result)
    elif model_id == 3:
        # 郭晴
        result = gq_en.predict()
        return jsonify(code=RETCODE.OK, msg="OK", data=result)
    elif model_id == 4:
        # 丁志君
        result = dzj_en.predict(text)
        return jsonify(code=RETCODE.OK, msg="OK", data=result)
    else:
        return jsonify(code=RETCODE.NODATA, msg="资源不存在！")


@api.route('/zh/<model_id>', methods=["POST"])
def zh_model(model_id):
    """
    中文模型接口
    :param model_id:
    :return: JSON数据
    """
    model_id = int(model_id)
    print(request)
    req = request.get_json()
    print(req)
    text = req.get("zh_text")
    # text = "无痛胃镜与普通胃镜区别"
    if model_id == 0:
        # 季杨
        result = jy_zh.predict(text)
        return jsonify(code=RETCODE.OK, msg="OK", data=result)
    elif model_id == 1:
        # 胡势权
        # result = hsq_zh.predict(text)
        result = hsq_zh.predict(text)

        return jsonify(code=RETCODE.OK, msg="OK", data=result)
    elif model_id == 2:
        # 何家乐
        result = hjl_zh.predict(text)
        return jsonify(code=RETCODE.OK, msg="OK", data=result)
    elif model_id == 3:
        # 郭晴
        result = gq_zh.predict()
        return jsonify(code=RETCODE.OK, msg="OK", data=result)
    elif model_id == 4:
        # 丁志君
        result = dzj_zh.predict(text)
        return jsonify(code=RETCODE.OK, msg="OK", data=result)
    else:
        return jsonify(code=RETCODE.NODATA, msg="资源不存在！")

# # 程序启动入口
# if __name__ == '__main__':
#     app.run()
